package com.nbjtjc.safe.activiti.listener;

import com.nbjtjc.safe.activiti.mapper.ExtActNodeMapper;
import com.nbjtjc.safe.activiti.model.ExtActNode;
import com.nbjtjc.safe.mapper.danger.DangerInspectPublicMapper;
import com.nbjtjc.safe.util.SpringContextUtils;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * @auther guqianbin
 * @date 2018/6/12 14:24
 **/
@Component("dangerFlowListener")
public class DangerFlowListener implements ExecutionListener, TaskListener, Serializable {
    @Value("${dangerInspect.finishValue}")
    private Integer finishValue;

    @Override
    public void notify(DelegateExecution delegateExecution) {
        if ("end".equals(delegateExecution.getEventName())) {
            DangerInspectPublicMapper dangerInspectPublicMapper =
                    SpringContextUtils.getBean(DangerInspectPublicMapper.class);
            Integer inspectId = (Integer) delegateExecution.getVariable("busKey");
            dangerInspectPublicMapper.finish(finishValue,inspectId);
        }
    }

    @Override
    public void notify(DelegateTask delegateTask) {
        ExtActNodeMapper extActNodeMapper = SpringContextUtils.getBean(ExtActNodeMapper.class);
        DangerInspectPublicMapper dangerInspectPublicMapper =
                SpringContextUtils.getBean(DangerInspectPublicMapper.class);
        if(delegateTask.getEventName().equals("create")){
            Integer inspectId = (Integer) delegateTask.getVariable("busKey");
            ExtActNode extActNode = extActNodeMapper.findByProcdefIdAndNodeId(delegateTask.getProcessDefinitionId(), delegateTask.getTaskDefinitionKey());
            dangerInspectPublicMapper.updateInspectStatus(extActNode.getStatus(),inspectId);
            if(extActNode.getPublicNotice()){
                //todo 发布通知
            }
        }
    }
}
